package api

import (
	"github.com/gin-gonic/gin"
	"go-iot-project/pkg/cache"
	"go-iot-project/pkg/curd"
	"time"
)

// @Summary		设置redis 值
// @Description	设置redis 值
// @Tags			redis
// @Accept			json
// @Produce		json
// @Param			input	body		map[string]any	true	"User information to create"
// @Success		200		{object}	string
// @Router			/api/redis/set [post]
func RedisSet(ctx *gin.Context) {
	var body map[string]any
	err := ctx.BindJSON(&body)
	if err != nil {
		curd.Error(ctx, err)
		return
	}
	rdbClient := cache.GetDefaultRedisClient()

	for k, v := range body {
		rdbClient.Client.Set(ctx, k, v, 60*time.Second)
	}
	curd.OK(ctx, gin.H{
		"data": "ok",
	})
}

// @Summary		读取redis 值
// @Description	读取redis 值
// @Tags			redis
// @Accept			json
// @Produce		json
// @Param			key	path		string	true	"redis key"
// @Success		200	{object}	string
// @Router			/api/redis/{key} [get]
func RedisGet(ctx *gin.Context) {
	key := ctx.Param("key")
	rdbClient := cache.GetDefaultRedisClient()
	val, _ := rdbClient.Client.Get(ctx, key).Result()
	ttl, _ := rdbClient.TTL(ctx, key).Result()
	curd.OK(ctx, gin.H{
		"data": val,
		"ttl":  ttl / time.Second,
	})
}
